package com.example.common.mvvm.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewbinding.ViewBinding;

import com.example.common.filed.DataActionEntity;
import com.example.common.mvvm.viewmodel.BaseViewModel;
import com.umeng.analytics.MobclickAgent;

import java.lang.ref.WeakReference;
import java.lang.reflect.ParameterizedType;
import java.util.Map;

public abstract class BaseActivity<V extends ViewBinding , VM extends BaseViewModel> extends AppCompatActivity implements IView {
    protected V v;
    protected VM vm;
    protected LayoutInflater inflater;
    private BusDataObserver busDataObserver;

    protected abstract String pageName();
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(""+pageName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(""+pageName());
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflater = LayoutInflater.from(this);
        busDataObserver = new BusDataObserver(this);
        v = (V) initBind();
        setContentView(bindLayout());

        initViewModel();
        getLifecycle().addObserver(vm);
        vm.vmData.uiData.observe(this, busDataObserver);
        initView();
        initData();

    }
    public abstract void receiveData(Map<String, Object> data);
    @Override
    protected void onDestroy() {
        super.onDestroy();
        getLifecycle().removeObserver(vm);
        vm.vmData.uiData.removeObserver(busDataObserver);
    }
    protected  void finishPage(){
        finish();
    }

    @Override
    public void initViewModel() {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Class clazz = (Class) type.getActualTypeArguments()[1];
        vm = (VM) new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory())
                .get(clazz);
    }

    public static class BusDataObserver implements androidx.lifecycle.Observer<DataActionEntity> {
        private WeakReference<BaseActivity> weakReference;

        public BusDataObserver(BaseActivity baseActivity) {
            weakReference = new WeakReference<>(baseActivity);
        }

        @Override
        public void onChanged(DataActionEntity entity) {

            if (weakReference.get() == null) {
                return;
            }
            if (entity.action.equals("DATA")){
                Map<String, Object> data = entity.data;
                weakReference.get().receiveData(data);
                return;
            }
            if (entity.action.equals("FINISHPAGE")){
                weakReference.get().finishPage();
                return;
            }
        }
    }

}
